package com.zp.demo.service;

import com.zp.demo.repush.RePushUtils;
import com.zp.demo.repush.anno.RePushAnno;
import com.zp.demo.repush.anno.RePushCutAnno;
import com.zp.demo.repush.service.impl.IsTrueStrategyKeyFunc;
import com.zp.demo.repush.service.impl.StrategyKeyFunc9999;
import com.zp.demo.service.impl.IsSucessTrue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class TestSetRedis {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    @RePushAnno("setRedis")
    public Boolean setRedis(String businessId) throws RuntimeException {
        try {
            boolean flag = redisTemplate.opsForValue().setIfAbsent(businessId, "ooxx");
            if (flag) {
                // 设置时间
                redisTemplate.expire(businessId, 3000, TimeUnit.MILLISECONDS);
                return flag;
            }
        } catch (Exception e) {
            String[] args = {businessId};
            RePushUtils.throwException(args, e.getClass().getName(), e.getMessage(), new IsSucessTrue());
        }
        String[] args = {businessId};
        // 抛出异常
        RePushUtils.throwException(args, "9999", "sss", new IsSucessTrue());
        return false;
    }

    @RePushCutAnno(value = "setRedis222222",isOkClazz = IsSucessTrue.class,strategyKey = StrategyKeyFunc9999.class)
    public Boolean setRedis2(String businessId) throws RuntimeException {
        boolean flag = redisTemplate.opsForValue().setIfAbsent(businessId, "ooxx");
        if (flag) {
            // 设置时间
            redisTemplate.expire(businessId, 20000, TimeUnit.MILLISECONDS);
            return flag;
        }
        return false;
    }
}
